package cn.nubia.admin.application.controller;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import cn.nubia.admin.application.bean.Feedback;
import cn.nubia.admin.application.query.FeedbackQuery;
import cn.nubia.admin.application.service.FeedbackService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by justin on 15/7/12.
 */
@Controller
@RequestMapping(value = "/admin/feedback/")
public class FeedbackController {

    @Autowired
    FeedbackService feedbackService;

    @RequiresPermissions(value = "soft:feedback:list")
    @RequestMapping(value = "list")
    public ModelAndView list(FeedbackQuery query){
        ModelAndView modelAndView = new ModelAndView("/feedback/feedbackList");
        Paged<Feedback> feedbacks = feedbackService.listFeedbacks(query);
        modelAndView.addObject("feedbacks",feedbacks);
        modelAndView.addObject("statusMap",Feedback.statusMap);
        modelAndView.addObject("query",query);
        return modelAndView;
    }

    @RequestMapping(value = "edit")
    public ModelAndView edit(Integer id){
        ModelAndView modelAndView = new ModelAndView("/feedback/feedbackForm");
        Feedback feedback = feedbackService.getFeedback(id);
        modelAndView.addObject("feedback",feedback);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public Response<Boolean> save(@Valid Feedback feedback, BindingResult result){
        if(result.hasErrors())return Response.getFailedResponse(result.getAllErrors().get(0).getDefaultMessage());
        feedbackService.processAndSave(feedback);
        return  Response.getSuccessResponse(true);
    }
}
